package com.joplus.housekeeper.bean;

import android.support.annotation.NonNull;
import android.util.Log;

import com.hyphenate.chat.EMImageMessageBody;
import com.hyphenate.chat.EMMessage;
import com.hyphenate.chat.EMTextMessageBody;
import com.hyphenate.chat.EMVoiceMessageBody;
import com.joplus.housekeeper.core.Config;

import java.io.File;

import io.realm.RealmObject;
import io.realm.annotations.PrimaryKey;


/**
 * Created by zisheng on 2018/6/27.
 */

public class MessageRecordListBean extends RealmObject {


    /**
     * accountId : acc14883811083461oTw
     * clientType : 2
     * content : VTEwMDAy
     * createDatetime : 1529912852470
     * messageDatetime : 1529909107276
     * messageFrom : customer-service3
     * messageId : f183807b688d42e7a6c61c8c9c22a7df
     * messageTo : acc14883811083461otw
     * sid : e7d97855ab6f4d8cbe15a66e20acdfa6
     * state : read
     * type : 8
     * decryptContent : 有人吗？
     */

    private String accountId;
    private int clientType;
    private String content;
    private long createDatetime;
    private long messageDatetime;
    private int duration;
    private String messageFrom;
    @PrimaryKey
    private String messageId;
    private String messageTo;
    private String sid;
    private String state;
    private int type;
    private String decryptContent;
    private String mobile;

    private String name;
    private String avatar;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getAvatar() {
        return avatar;
    }

    public void setAvatar(String avatar) {
        this.avatar = avatar;
    }

    public String getMobile() {
        return mobile;
    }

    public void setMobile(String mobile) {
        this.mobile = mobile;
    }

    public String getAccountId() {
        return accountId;
    }

    public int getDuration() {
        return duration;
    }

    public void setDuration(int duration) {
        this.duration = duration;
    }

    public void setAccountId(String accountId) {
        this.accountId = accountId;
    }

    public int getClientType() {
        return clientType;
    }

    public void setClientType(int clientType) {
        this.clientType = clientType;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    public long getCreateDatetime() {
        return createDatetime;
    }

    public void setCreateDatetime(long createDatetime) {
        this.createDatetime = createDatetime;
    }

    public long getMessageDatetime() {
        return messageDatetime;
    }

    public void setMessageDatetime(long messageDatetime) {
        this.messageDatetime = messageDatetime;
    }

    public String getMessageFrom() {
        return messageFrom;
    }

    public void setMessageFrom(String messageFrom) {
        this.messageFrom = messageFrom;
    }

    public String getMessageId() {
        return messageId;
    }

    public void setMessageId(String messageId) {
        this.messageId = messageId;
    }

    public String getMessageTo() {
        return messageTo;
    }

    public void setMessageTo(String messageTo) {
        this.messageTo = messageTo;
    }

    public String getSid() {
        return sid;
    }

    public void setSid(String sid) {
        this.sid = sid;
    }

    public String getState() {
        return state;
    }

    public void setState(String state) {
        this.state = state;
    }

    public int getType() {
        return type;
    }

    public void setType(int type) {
        this.type = type;
    }

    public String getDecryptContent() {
        return decryptContent;
    }

    public void setDecryptContent(String decryptContent) {
        this.decryptContent = decryptContent;
    }


    public EMMessage change2EmMessage() {
        if (getType() == 2) {
            EMMessage voiceSendMessage;

            if (!getMessageFrom().contains("customer") && !getMessageFrom().equals("easemob") && !getMessageFrom().equals("kefu")) {
                voiceSendMessage = createReceiveMessage(getContent(), getDuration(), getMessageTo());
            } else {

                //      voiceSendMessage.setDirection(EMMessage.Direct.SEND);
                voiceSendMessage = EMMessage.createVoiceSendMessage(getContent(), getDuration(), getMessageTo());
                voiceSendMessage.setDirection(EMMessage.Direct.RECEIVE);}

            voiceSendMessage.setMsgId(getMessageId());
            voiceSendMessage.setChatType(EMMessage.ChatType.Chat);
            voiceSendMessage.setFrom(getMessageFrom());
            voiceSendMessage.setTo(getMessageTo());
            if (getMessageDatetime() != 0) {
                voiceSendMessage.setMsgTime(getMessageDatetime());
            }

            voiceSendMessage.setStatus(EMMessage.Status.SUCCESS);

            return voiceSendMessage;

        } else if (getType() == 3) {
            EMMessage message;
            if (!getMessageFrom().contains("customer") && !getMessageFrom().equals("easemob")&& !getMessageFrom().equals("kefu")) {
                message = EMMessage.createReceiveMessage(EMMessage.Type.IMAGE);
            } else {

                message = EMMessage.createSendMessage(EMMessage.Type.IMAGE);
                message.setDirection(EMMessage.Direct.RECEIVE);            }
            message.setFrom(getMessageFrom());
            message.setTo(getMessageTo());
            message.addBody(new EMImageMessageBody(new File(getContent())));
            message.setChatType(EMMessage.ChatType.Chat);
            message.setMsgTime(getMessageDatetime());
            message.setMsgId(getMessageId());
            return message;
        } else {
            if (!getMessageFrom().startsWith("customer") && !getMessageFrom().equals("easemob")&& !getMessageFrom().equals("kefu")) {


                EMMessage message = EMMessage.createReceiveMessage(EMMessage.Type.TXT);

                message.setFrom(getMessageFrom());
                message.setTo(getMessageTo());
                message.addBody(new EMTextMessageBody(getContent()));
                message.setChatType(EMMessage.ChatType.Chat);
                message.setMsgTime(getMessageDatetime());
                message.setMsgId(getMessageId());

                return message;
            } else {
                EMMessage message = EMMessage.createSendMessage(EMMessage.Type.TXT);
                message.setDirection(EMMessage.Direct.SEND);
                message.setFrom(getMessageFrom());
                message.setTo(getMessageTo());
                message.addBody(new EMTextMessageBody(getContent()));
                message.setChatType(EMMessage.ChatType.Chat);
                message.setMsgTime(getMessageDatetime());
                message.setMsgId(getMessageId());

                return message;
            }


        }


    }


    public EMMessage createReceiveMessage(String filePath, int length, String toChatUsername) {
        EMMessage receiveMessage = EMMessage.createReceiveMessage(EMMessage.Type.VOICE);
        EMVoiceMessageBody emVoiceMessageBody = new EMVoiceMessageBody(new File(filePath), length);
        receiveMessage.addBody(emVoiceMessageBody);
        receiveMessage.setTo(toChatUsername);
        receiveMessage.setChatType(EMMessage.ChatType.Chat);
        receiveMessage.setFrom(Config.CUSTOM_SERVICE_IMID);
        receiveMessage.setMsgTime(System.currentTimeMillis());
        return receiveMessage;
    }


}
